const query = require('./fun')
const sqlCount = 'SQL_CALC_FOUND_ROWS';
const { SWIPER, MENU, RTICLEATYPE, USER, RTICLEA, MENUTYPE, PRAISE, FOLLOW, ADMIN, MOTION, PLAN } = require('./database')
let { sqlUpdateStr, sqlAddStr, sqlSelectStr, sqlWhereStr } = require('../util.js');
/**
 * 判断是否存在用户
 */
function isUser(data) {
    return new Promise((resolve, reject) => {
        query(`select  * from ${USER} where openid=?`, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 添加用户
 */
function addUser(data) {
    return new Promise((resolve, reject) => {
        query(`INSERT INTO ${USER} (userId,openid,session_key) VALUES(null,?,?) `, data)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 修改用户信息
 */
function updataUser(userId, data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        Object.entries(data).forEach(item => {
            if (sql.toString()) {
                sql += ',';
            }
            sql += `${item[0]}='${item[1]}'`
        })
        query(`UPDATE ${USER} SET ${sql} WHERE userId=${userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 根据用户的id返回用户信息
 */
function GetUsersById(id) {
    return new Promise((resolve, reject) => {
        query(`select userName,userUrl,userSex,userId from ${USER} where userId=?`, { id })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取所有轮播图
 */
function getSwiperList() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${SWIPER} ORDER BY swiperIndex ASC`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 返回首页推荐的菜谱
 */
function getRecommendList(num) {
    return new Promise((resolve, reject) => {
        query(`select  ${sqlCount} * from ${MENU}  order by menuId desc limit ${num}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取热门菜谱
 */
function getHotRecommendList(num) {
    return new Promise((resolve, reject) => {
        query(`select  ${sqlCount} * from ${MENU}  order by menuPraise desc limit ${num}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取文章分类
 */
function getRticleatypeList() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${RTICLEATYPE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取指定分类的文章
 */
function getRticleabyType(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, like);
        query(`select  ${sqlCount} * from ${RTICLEA} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取菜谱分类
 */
function getMenutypeList() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${MENUTYPE}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取指定分类的菜谱信息
 */
function getMenuBytype(like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, like);
        query(`select  ${sqlCount} * from ${MENU} ${condition}`, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据指定时区获取菜谱信息
 */
function getMenuByTime(like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, like);
        query(`select  ${sqlCount} * from ${MENU} ${condition}`, {}, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取菜谱信息
 */
function getMenuById(menuId) {
    return new Promise((resolve, reject) => {
        query(`select * from ${MENU} where menuId=?`, { menuId })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 判断是否点赞
 */
function isaddPraise(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${PRAISE} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 取消点赞
 */
function delPraise(praiseId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${PRAISE} WHERE praiseId=?`, { praiseId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 点赞
 */
function addPraise(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${PRAISE}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 菜谱点赞数目改变
 */
function changeMenuPraise(data) {
    return new Promise((resolve, reject) => {
        let sql = `menuPraise=menuPraise+${data.PraiseNum}`;
        query(`UPDATE ${MENU} SET ${sql} WHERE menuId=${data.menuId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 判断是否关注收藏
 */
function isaaddFollow(data) {
    return new Promise((resolve, reject) => {
        let sql = '';
        sql = sqlWhereStr(sql, data)
        query(`select * from ${FOLLOW} ${sql}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 取消关注收藏
 */
function delFollow(followId) {
    return new Promise((resolve, reject) => {
        query(`DELETE FROM ${FOLLOW} WHERE followId=?`, { followId })
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 关注收藏
 */
function addFollow(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${FOLLOW}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据id获取文章信息
 */
function getRticleaById(rticleaId) {
    return new Promise((resolve, reject) => {
        query(`select ${RTICLEA}.*,${ADMIN}.name as adminName from ${RTICLEA},${ADMIN} where rticleaId=? and ${ADMIN}.id=${RTICLEA}.adminId`, { rticleaId })
            .then((res) => {
                resolve(res[0])
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 菜谱阅读数目改变
 */
function changeRticleaRead(data) {
    return new Promise((resolve, reject) => {
        let sql = `rticleaRead=rticleaRead+${data.rticleaRead}`;
        query(`UPDATE ${RTICLEA} SET ${sql} WHERE rticleaId=${data.rticleaId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(reject)
            })
    })
}
/**
 * 获取用户点赞的菜谱
 */
function getUserPraiseMenu(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${PRAISE}.userId`]: like.userId,
            [`${PRAISE}.menuId`]: `${MENU}.menuId`
        });
        query(`select  ${sqlCount} ${MENU}.* from ${MENU},${PRAISE} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取用户收藏的菜谱
 */
function getUserFollowMenu(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlWhereStr(condition, {
            [`${FOLLOW}.userId`]: like.userId,
            [`${FOLLOW}.menuId`]: `${MENU}.menuId`
        });
        query(`select  ${sqlCount} ${MENU}.* from ${MENU},${FOLLOW} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据菜谱名称获取菜谱
 */
function getMenuByName(num, limit, like) {
    return new Promise((resolve, reject) => {
        let condition = '';
        condition = sqlSelectStr(condition, like);
        query(`select  ${sqlCount} * from ${MENU} ${condition}  limit ?,${limit}`, { num }, true)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取运动的所有数据
 */
function getMotionList() {
    return new Promise((resolve, reject) => {
        query(`select *  from ${MOTION}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 获取全部菜谱
 */
function getMenuAll() {
    return new Promise((resolve, reject) => {
        query(`select * from ${MENU}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 添加用户周计划
 */
function addPlan(data) {
    return new Promise((resolve, reject) => {
        let obj = sqlAddStr(data);
        query(`INSERT INTO ${PLAN}(${obj.keys}) VALUES(${obj.vals}) `)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
/**
 * 根据用户id获取周计划
 */
function getWeekPlan(userId) {
    return new Promise((resolve, reject) => {
        query(`select *  from ${PLAN} where userId=${userId}`)
            .then((res) => {
                resolve(res)
            })
            .catch((err) => {
                reject(err)
            })
    })
}
exports.isUser = isUser;
exports.addUser = addUser;
exports.updataUser = updataUser;
exports.GetUsersById = GetUsersById;
exports.getSwiperList = getSwiperList;
exports.getRecommendList = getRecommendList;
exports.getRticleatypeList = getRticleatypeList;
exports.getRticleabyType = getRticleabyType;
exports.getMenutypeList = getMenutypeList;
exports.getMenuBytype = getMenuBytype;
exports.getMenuByTime = getMenuByTime;
exports.getMenuById = getMenuById;
exports.isaddPraise = isaddPraise;
exports.delPraise = delPraise;
exports.addPraise = addPraise;
exports.changeMenuPraise = changeMenuPraise;
exports.isaaddFollow = isaaddFollow;
exports.delFollow = delFollow;
exports.addFollow = addFollow;
exports.getHotRecommendList = getHotRecommendList;
exports.getRticleaById = getRticleaById;
exports.changeRticleaRead = changeRticleaRead;
exports.getUserPraiseMenu = getUserPraiseMenu;
exports.getUserFollowMenu = getUserFollowMenu;
exports.getMenuByName = getMenuByName;
exports.getMotionList = getMotionList;
exports.getMenuAll = getMenuAll;
exports.addPlan = addPlan;
exports.getWeekPlan = getWeekPlan;